package huixue.cn.com.rxapp.login.present;

import android.content.Intent;
import android.text.TextUtils;



import huixue.cn.com.library.entity.BaseHttpResult;
import huixue.cn.com.library.observer.BaseObserver;
import huixue.cn.com.library.util.HttpUtil;
import huixue.cn.com.library.util.JsonUtil;
import huixue.cn.com.library.util.ToastUtils;
import huixue.cn.com.rxapp.R;
import huixue.cn.com.rxapp.api.RetrofitService;
import huixue.cn.com.rxapp.base.BasePresent;
import huixue.cn.com.rxapp.base.IBaseUI;
import huixue.cn.com.rxapp.entity.HttpResult;
import huixue.cn.com.rxapp.login.entity.User;
import huixue.cn.com.rxapp.user.UserSingle;
import huixue.cn.com.rxapp.user.ui.HomeActivity;
import huixue.cn.com.rxapp.util.HttpUiTips;
import huixue.cn.com.rxapp.util.LogUtils;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/7/29.
 */

public class LoginPresenter extends BasePresent<LoginPresenter.LoginUI> {

    public interface  LoginUI extends IBaseUI{

    }


    public void login(String loginName,String password){
        if (TextUtils.isEmpty(loginName)) {
            ToastUtils.showToast("请输入" + getString(R.string.login_hint_username));
            return;
        }

        if (TextUtils.isEmpty(password)) {
            ToastUtils.showToast("请输入" + getString(R.string.login_hint_username));
            return;
        }
        HttpUiTips.showDialog(getActivity(),true, "loading...");

        io.reactivex.Observable.create(new ObservableOnSubscribe<Integer>() {
            int i = 0;

            @Override
            public void subscribe(@NonNull ObservableEmitter<Integer> e) throws Exception {
                while (i < 3) {
                    LogUtils.e("MainActivity", "Observable on " + Thread.currentThread().getName() + " emit " + i);
                    e.onNext(i);
                    i++;
                }
            }
        }).subscribeOn(Schedulers.io())
                //map操作符，Function<Object,Object>，只要类型为Object的子类就可以进行转换
        .map(new Function<Integer, String>() {
                    //这个就是转换的函数，返回的是转换结果
                    @Override
                    public String apply(@NonNull Integer integer) throws Exception {
                        return "This is a String Type:" + integer;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                /*.subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
                        //这里接收的就是一个String类型了
                        LogUtils.e("MainActivity", "Observer on " + Thread.currentThread().getName() + " Receive:" + s);
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                })*/;

        BaseObserver<String> observer = new BaseObserver<String>(getActivity()) {
            @Override
            public void onSuccess(String content) {
                super.onSuccess(content);
                HttpUiTips.dismissDialog(getActivity());
                ToastUtils.showToast("onSuccess" );
                User user = JsonUtil.fromJson(content,User.class);
                UserSingle._getInstance()
                        .setUser(
                        user);

                //ARouter.getInstance().build("/com/homeActivity").navigation();
                Intent intent = new Intent(getActivity(), HomeActivity.class);
                getActivity().startActivity(intent);
            }

            @Override
            public void onError(Throwable t) {
                super.onError(t);
                ToastUtils.showToast("onError" );
            }
        };




         RetrofitService.login(loginName,password)
                .observeOn(Schedulers.io())
                .map(new Function<BaseHttpResult<String>, User>() {
                    @Override
                    public User apply(BaseHttpResult<String> stringBaseHttpResult) throws Exception {
                        User user = JsonUtil.fromJson(stringBaseHttpResult.getResContent(),User.class);
                        return user;
                    }
                })
                .flatMap(new Function<User, ObservableSource<BaseHttpResult<String>>>() {
                    public ObservableSource<BaseHttpResult<String>> apply(User user) throws Exception {
                        return null;
                    }
                }).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
    }


}
